import requests
import json
import re
import MySQLdb
class weizan:

	def __int__(self):

		pass

	def postHuaTi(self,limit = 5):

		url = 'https://live.vzan.com/nlive/GetTopicChannData?ver=0.358119744904291'

		params = {"page":"1","limit":limit,"type":"topic","zid":"1927743619","idOrname":"","paytype":"全部"}

		header = {'Cookie':'zbArticleCookies=articleId=484&lastEditTime=2020-08-18 10:07:39; CustomTip237895154_1927743619=1; UserCookieNew=8b671b40-0c26-4bb5-b26d-dfdfb184b06a; AccountVaild=Vaild_vzan91706467a66b7345-7986-4f25-829c-c8a0f51b7ced; notshowuserlist=empty; notshowshadow=empty; LoginUserName=18926445002'}

		r = requests.post(url=url,data=params,headers=header)

		obj = json.loads(r.text)

		return obj


	def postDingDan(self):

		url = 'https://assets.vzan.com/api/OrderService/OrderList'

		params = {"isRecord":'false',"orderIds":[],"startTime":"2020-09-03 00:00:00","endTime":"2020-09-04 23:59:59","orderType":'null',"status":'null',"tradeStatus":'null',"accountType":'null',"refundStatus":'null',"deliveryType":0,"source":"全部","shareId":0,"pageIndex":1,"pageSize":10}

		header = {'Content-Type':'application/json text/plain','lid':'1927743619','Cookie':'zbArticleCookies=articleId=484&lastEditTime=2020-08-18 10:07:39; CustomTip237895154_1927743619=1; UserCookieNew=8b671b40-0c26-4bb5-b26d-dfdfb184b06a; AccountVaild=Vaild_vzan91706467a66b7345-7986-4f25-829c-c8a0f51b7ced; notshowuserlist=empty; notshowshadow=empty; LoginUserName=18926445002'}

		r = requests.post(url=url,data=params,headers=header)

		obj = json.loads(r.text)

		print(obj)

	def postHuatiInfo(self,zid):

		url='https://live.vzan.com/nlive/GetTopicanalysis'

		header = {'Cookie':'zbArticleCookies=articleId=484&lastEditTime=2020-08-18 10:07:39; CustomTip237895154_1927743619=1; UserCookieNew=8b671b40-0c26-4bb5-b26d-dfdfb184b06a; AccountVaild=Vaild_vzan91706467a66b7345-7986-4f25-829c-c8a0f51b7ced; notshowuserlist=empty; notshowshadow=empty; LoginUserName=18926445002'}
		
		params ={'zid':zid,'tid':zid}

		r = requests.post(url=url,data=params,headers=header)

		obj = json.loads(r.text)

		return obj


	def insertDb(self,str):

			#INSERT IGNORE
		query = 'REPLACE INTO huatiorpindaoid (pinDaoId,huaTiId,kaiBoTime,title,boFangShiChang,topuv,tipper,profit) VALUES '+ str ;

		url = 'http://cscs.qumei.com:30004/api/index.php?type=1&query=' + query 

		r = requests.get(url = url)

		obj = json.loads(r.text[1:500 * 10000])

		# print('insert=>',jsonstr)

		return obj

	def wzCaoZuo(self,limit):

		htList = self.postHuaTi(limit)

		if(htList['code']=='0'):

			dataList = htList['data']

			for i in range(0,len(dataList)-1):

				data = dataList[i]

				IdInfo = self.postHuatiInfo(data['Id'])['dataObj']

				stri = '(\'' + str(data['cId'])+'\',\'' + str(data['Id'])+ '\',\'' + data['addtime']+ '\',\'' + data['title']+ '\',\'' + str(IdInfo['v_live'])+ '\',\'' + str(IdInfo['toppv'])+ '\',\'' +str(IdInfo['tipper'])+ '\',\'' + str(IdInfo['profit']) + '\')'

				stri = re.sub(r'&','',stri)

				res = self.insertDb(stri)['data']

				if res: 

					rt ='插入成功' 
				else: 
					rt = '插入失败'

				print('执行进度=>', str(int(((i+1) / (len(dataList)-1)) * 100)) + '% ,' + stri + ',' + rt )


if __name__ == '__main__':

	wz = weizan()

	

	wz.wzCaoZuo(50)

	